-- Adventurer
-- Created by chengb Aug/2/2016
-- 冒险者

local MAX_DIALOG_NUM = 4;       -- 对话数量
local ADVENTURER_PET_ID = 1;    -- 冒险者冈布奥编号

-- 内部函数声明
local getDialogContent;

-- 初始化
function initAdventurer(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local iconPath = getHeroIconPath(ADVENTURER_PET_ID);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local function okFunc()
                local msgList = { getDialogContent(self), };
                EventMgr.fire(event.SLIME_TALK, {["heroId"] = 1,
                    ["msgList"] = msgList, ["heroName"] = getLocStr("adventurer"), });
            end

            AudioM.playFx("ui_open1");
            self:openBuildingView2(okFunc, nil, nil, nil, nil, iconPath);
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:setScale(1.2);
    self.item:setPositionY(-10);

    -- 呼吸
    self.needAnimation = true;
end

-- 获取对话内容
function getDialogContent(self)
    local elementDbase = DungeonM.queryElement(self.gridData.element, "dbase");
    local dialogs = elementDbase["dialogs"];

    assert(type(dialogs) == 'table', "必须配置冒险者对话");

    local maxDialogNum = #dialogs;
    local lastIndex = ME.user.dbase:queryTemp("adventurer_dialog_index") or 0;
    local curIndex = 1 + lastIndex  % maxDialogNum;
    ME.user.dbase:setTemp("adventurer_dialog_index", curIndex);

    return getLocStr(dialogs[curIndex]);
end